from typing import Any, Optional, Awaitable

import tornado.web
from tornado import web

import  json
# def func():
#     def func_inner():
#         print("1")
#     return func_inner




# 安全cookies的shiyong
class CookieCountHandler(web.RequestHandler):
    def get(self):
        count = self.get_secure_cookie("page_count")
        if not count:
            self.set_secure_cookie("page_count", "1")
            count = 1
        else:
            count = int(count)
            count += 1
            self.set_secure_cookie("page_count", str(count))
        self.write(str(count))


class IndexHandler1(web.RequestHandler):
    def initialize(self):
        print(self.application.db)
        print(self.application.db_obj)

    #     # 自定义
    #     def write_error(self, status_code: int, **kwargs) -> None:
    #         """
    #         err_title :错误标题
    #         err_content:错误描述
    #         :param status_code:
    #         :param kwargs:
    #         :return:
    #         """
    #         self.write("出错了%s" % kwargs.get("err_title", ""))
    #         self.write("出错了%s" % kwargs.get("err_content", ""))

    def get_current_user(self) -> Any:
        # 验证用户名密码
        # cun fang zhuang tai
        return False

    @tornado.web.authenticated
    def get(self):
        # self.write(f"{self.db}")
        self.set_status(400, reason="出个错")
        self.set_header("app", "my_self")
        # 抛出错误
        self.send_error(404, error_content="abc")  # 之后不在执行
        self.write_error(500)

        self.finish({'sleep   tornado!!9kjh99': f'sdf'})

    def post(self):
        # count = self.get_secure_cookie("page_count")
        # if not count:
        #     self.set_secure_cookie("page_count", "1")
        #     count = 1
        # else:
        #     count = int(count)
        #     count += 1
        #     self.set_secure_cookie("page_count", str(count))
        # self.write(str(count))
        print(self.xsrf_token)
        self.write("aaaaaa")


class ProfileHandler(web.RequestHandler):
    def get_current_user(self) -> Any:
        # 在此按成用户ren zehgn luo ji
        user_name = self.get_argument("name", None)
        return user_name

    @tornado.web.authenticated
    def get(self):
        self.write("ge ren ren zheng")


class LoginHandler(web.RequestHandler):

    def get(self):
        next_url = self.get_argument("next", "")
        if next_url:
            self.redirect(next_url + "?f=login")
        self.write("login.html")
        self.render("index.html")

class IndexTestHandler(web.RequestHandler):

    # @tornado.httpclient.AsyncHTTPClient
    # def get(self):
    #     client  = tornado.httpclient.AsyncHTTPClient
    #     client.fetch("外部url",callable=self.on_response)
    #
    # def on_response(self,resp) -> None:
    #     json_data=resp.body
    #     data = json.loads(json_data)
    #     self.write(data)

    @tornado.gen.coroutine
    def get(self):
        client = tornado.httpclient.AsyncHTTPClient
        resp = yield client.fetch("外部url")
        raise tornado.gen.Return("异常")

    def on_response(self, resp) -> None:
        json_data = resp.body
        data = json.loads(json_data)
        self.write(data)

